package com.cs.service.article;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cs.mapper.ArticleMapper;

import com.cs.pojo.Article;
import com.cs.pojo.CarouselMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public IPage<Article> getPage(int currentPage, int pageSize) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<Article> page=new Page<>(currentPage,pageSize);
        articleMapper.selectPage(page,lambdaQueryWrapper);
        return page;
    }

    @Override
    public List<Article> getByTitle(String title) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Article::getTitle,title);
        List<Article> articleList = articleMapper.selectList(lambdaQueryWrapper);
        return articleList;
    }

    @Override
    public IPage<Article> getPageByTitle(int currentPage, int pageSize, String title) {
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Article::getTitle,title);
        IPage<Article> page=new Page<>(currentPage,pageSize);
        articleMapper.selectPage(page,lambdaQueryWrapper);
        return page;
    }

    @Override
    public Article getInUsing() {
        LambdaQueryWrapper<Article> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Article::getIsUsing,1);
        Article article = articleMapper.selectOne(lambdaQueryWrapper);
        return article;
    }
    @Override
    public Boolean setUsingArticle(Integer id){
        Article inUsing = getInUsing();
        if(inUsing!=null){
            inUsing.setIsUsing(0);
            articleMapper.updateById(inUsing);
        }
        Article article = articleMapper.selectById(id);
        article.setIsUsing(1);
        int i = articleMapper.updateById(article);
        if (i>0){
            return true;
        }else {
            return false;
        }
    }

}
